#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <unordered_map>
#include <unistd.h>

class Session
{
public:
    Session(const std::string& username, const std::string& status)
    :_username(username)
    ,_status(status)
    {
        _create_time = time(nullptr);
    }
    ~Session()
    {}

    std::string _username;
    std::string _status;
    uint64_t _create_time;
    uint64_t _time_out; 
};

using session_ptr = std::shared_ptr<Session>;

class SessionManager
{
public:
    SessionManager()
    {
        srand(time(nullptr)^getpid());
    }

    std::string AddSession(session_ptr s)
    {
        uint32_t randomid = rand() + time(nullptr);
        std::string sessionid = std::to_string(randomid);
        _sessions.insert(std::make_pair(sessionid, s));
        return sessionid;
    }
    session_ptr GetSession(const std::string& sessionid)
    {
        if (_sessions.find(sessionid) == _sessions.end()) return nullptr;
        return _sessions[sessionid];
    }
    ~SessionManager()
    {}
private:
    std::unordered_map<std::string, session_ptr> _sessions;
};